<template>
  <j-modal
    :confirmLoading="confirmLoading"
    :dialogStyle="dialogStyle"
    :title="title"
    :visible="visible"
    :width="1200"
    cancelText="关闭"
    switchFullscreen
    @cancel="handleCancel"
    @ok="handleOk">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-row :gutter="16">
          <a-col :span="12">
            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              label="地区">
              <a-select v-decorator="['area', {
            rules: [
              {
                required: true,
                message: '地区不能为空!',
              },
            ],
            }]" placeholder="请选择地区" @change="handleChange">
<!--                <a-select-option value="全市">全市</a-select-option>-->
                <a-select-option value="黄浦区">黄浦区</a-select-option>
                <a-select-option value="徐汇区">徐汇区</a-select-option>
                <a-select-option value="长宁区">长宁区</a-select-option>
                <a-select-option value="静安区">静安区</a-select-option>
                <a-select-option value="普陀区">普陀区</a-select-option>
                <a-select-option value="虹口区">虹口区</a-select-option>
                <a-select-option value="杨浦区">杨浦区</a-select-option>
                <a-select-option value="闵行区">闵行区</a-select-option>
                <a-select-option value="宝山区">宝山区</a-select-option>
                <a-select-option value="嘉定区">嘉定区</a-select-option>
                <a-select-option value="浦东新区">浦东新区</a-select-option>
                <a-select-option value="金山区">金山区</a-select-option>
                <a-select-option value="松江区">松江区</a-select-option>
                <a-select-option value="青浦区">青浦区</a-select-option>
                <a-select-option value="奉贤区">奉贤区</a-select-option>
                <a-select-option value="崇明区">崇明区</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item
              :labelCol="labelCol2"
              :wrapperCol="wrapperCol"
              label="企业名称">
              <a-input v-decorator="['enterpriseName', {
            rules: [
              {
                required: true,
                message: '企业名称不能为空！'
              },
            ],
            }]" placeholder="请输入企业名称"/>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="16">
          <a-col :span="12">
            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              label="社会统一信用代码">
              <a-input v-decorator="['enterpriseUscc', {
           rules: [
              {
                required: true,
                message: '社会统一信用代码不能为空！',
              },
            ],
            }]" placeholder="请输入社会统一信用代码" suffix=""/>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item
              :labelCol="labelCol2"
              :wrapperCol="wrapperCol"
              label="能耗">
              <a-input v-decorator="['eneygyNum', {
           rules: [
              {
                required: true,
                message: '仅能输入数字和两位小数，不能为空或为负数！',
                pattern: /^[0-9]+\.?[0-9]{0,2}$/
              },
            ],
            }]" placeholder="请输入能耗值" suffix="吨"/>
            </a-form-item>
          </a-col>
        </a-row>
          <a-row :gutter="16">
            <a-col :span="12">
              <a-form-item
                :labelCol="labelCol"
                :wrapperCol="wrapperCol"
                label="排放量">
                <a-input v-decorator="['carbonNum', {
           rules: [
              {
                required: true,
                message: '仅能输入数字和两位小数，不能为空或为负数！',
                pattern: /^[0-9]+\.?[0-9]{0,2}$/
              },
            ],
            }]" placeholder="请输入排放量" suffix="吨"/>
              </a-form-item>
            </a-col>
            <a-col :span="12">
              <a-form-item
                :labelCol="labelCol2"
                :wrapperCol="wrapperCol"
                label="行业大类">
                <a-input v-decorator="['industryType', {
           rules: [
              {
                required: true,
                message: '行业大类不能为空！',
              },
            ],
            }]" placeholder="请输入行业大类" suffix=""/>
              </a-form-item>
            </a-col>
          </a-row>
        <a-row :gutter="16">
          <a-col :span="12">
            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              label="细分行业">
              <a-input v-decorator="['mince', {
           rules: [
              {
                required: true,
                message: '细分行业不能为空！',
              },
            ],
            }]" placeholder="请输入细分行业" suffix=""/>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item
              :labelCol="labelCol2"
              :wrapperCol="wrapperCol"
              label="煤">
              <a-input v-decorator="['coal', {
           rules: [
              {
                required: true,
                 message: '仅能输入数字或四位小数，不能为空！',
                pattern: /^-?\d+(\.\d{1,4})?$/
              },
            ],
            }]" placeholder="请输入煤的值" suffix="万吨"/>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="16">
          <a-col :span="12">
            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              label="油">
              <a-input v-decorator="['oil', {
           rules: [
              {
                required: true,
               message: '仅能输入数字或四位小数，不能为空！',
                pattern: /^-?\d+(\.\d{1,4})?$/
              },
            ],
            }]" placeholder="请输入油的值" suffix="万吨"/>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item
              :labelCol="labelCol2"
              :wrapperCol="wrapperCol"
              label="气">
              <a-input v-decorator="['gas', {
           rules: [
              {
                required: true,
                 message: '仅能输入数字或四位小数，不能为空！',
                pattern: /^-?\d+(\.\d{1,4})?$/
              },
            ],
            }]" placeholder="请输入气的值" suffix="万吨"/>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="16">
          <a-col :span="12">
            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              label="电">
              <a-input v-decorator="['electricity', {
           rules: [
              {
                required: true,
                message: '仅能输入数字或四位小数，不能为空！',
                pattern: /^-?\d+(\.\d{1,4})?$/
              },
            ],
            }]" placeholder="请输入电的值" suffix="万千瓦时"/>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item
              :labelCol="labelCol2"
              :wrapperCol="wrapperCol"
              label="蒸汽">
              <a-input v-decorator="['steam', {
           rules: [
              {
                required: true,
                 message: '仅能输入数字或四位小数，不能为空！',
                pattern: /^-?\d+(\.\d{1,4})?$/
              },
            ],
            }]" placeholder="请输入蒸汽的值" suffix="万吨"/>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>
import pick from 'lodash.pick'
import { httpAction } from '@api/manage'

export default {
  name: 'enterpriseRankModal',
  data() {
    return {
      confirmLoading: false,
      title: '',
      visible: false,
      form: this.$form.createForm(this),
      currentTime: '',
      dialogStyle: {
        top: '300px'
        // right:"20px",
      },
      labelCol: {
        // xs: { span: 24 },
        // sm: { span: 5 }
        span: 6
      },
      wrapperCol: {
        // xs: { span: 24 },
        // sm: { span: 16 }
        span: 16
      },
      labelCol2: {
        // xs: { span: 24 },
        // sm: { span: 5 }
        span: 4
      },
      url: {
        add: '/carbon/enterpriseFileConsumptionRankList/add',
        edit: '/carbon/enterpriseFileConsumptionRankList/edit'
      }
    }
  }, methods: {
    add() {
      this.edit({})
    },
    edit(record) {
      console.log(record)
      this.visible = true
      this.form.resetFields()
      this.model = Object.assign({}, record)
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(
          this.model,
          'area',
          'enterpriseName',
          'enterpriseUscc',
          'eneygyNum',
          'carbonNum',
          'industryType',
          'mince',
          'coal',
          'oil',
          'gas',
          'electricity',
          'steam',
        ))
      })
    },
    handleChange(value) {
      console.log(value)
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleCancel() {
      this.close()
    },
    handleOk() {
      let that = this
      this.form.validateFields((err, values) => {
        if (!err) {
          let httpurl = ''
          let method = ''
          this.confirmLoading = true
          console.log(this.model)
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          console.log(httpurl)
          let formData = Object.assign(this.model, values)
          httpAction(httpurl, formData, method).then(res => {
            if (res.success) {
              that.$message.success(res.message)
              that.$emit('refresh')
            } else {
              that.$message.error(res.message)
            }
          }).finally(() => {
            that.confirmLoading = false
            that.close()
            that.$parent.$parent.loadData()
          })
        }
      })
    }
  }
}
</script>

<style scoped>

</style>